package com.school.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.school.pojo.Account;
import com.school.pojo.File;
import com.school.pojo.Teacher;
import com.school.service.AccountService;
import com.school.service.FileService;
import com.school.service.TeacherService;
import com.school.util.Result;
import com.school.util.Tool;

@RequestMapping("/teacher")
@RestController
public class TeacherController implements Tool<Teacher>{
	
	@Autowired
	private TeacherService teacherService;
	
	@Autowired
	private AccountService accountService;
	
	@Autowired
	private FileService fileService;
	
	@Override
	@RequestMapping("/findAll")
	public Result<Teacher> findAll(@RequestParam(defaultValue = "1",name="page")int page, @RequestParam(defaultValue = "10",name="limit")int pageSize, Teacher t) {
		List<Teacher> list= teacherService.findAll(page,pageSize,t);
		PageInfo<Teacher> info=new PageInfo<>(list);
		Result<Teacher> result=new Result<Teacher>();
		result.setList(info.getList());
		result.setCount(info.getTotal());
		return result;
	}

	@Override
	@RequestMapping("/findbyId")
	public Result<Teacher> findbyId(int teacher_id) {
		
		Teacher teacher= teacherService.findbyId(teacher_id);
		
		Account account= accountService.findById(teacher.getA_id());
		
		File file= fileService.findbyId(teacher.getFile_id());
		
		System.out.println(file);
		teacher.setUsername(account.getUsername());
		teacher.setPassword(account.getPassword());
		teacher.setUrl(file.getUrl());
		teacher.setStatus(account.getStatus());
		
		Result<Teacher> result=new Result<Teacher>();
		result.setData(teacher);
		return result;
	}

	@Override
	@RequestMapping("/add")
	public int add(Teacher teacher) {
		//添加账号
		int i=accountService.add(teacher);
		System.out.println(i);
		if(i!=0) {
			//添加用户信息
			return teacherService.add(teacher);
		}
		
		
		return 0;
	}

	@Override
	@RequestMapping("/edit")
	public int edit(Teacher t) {
		//修改老师用户内容
		int a=teacherService.edit(t);
		//修改图片
		int b=fileService.edit(t);
		//修改密码
		int c=accountService.edit(t);
		if(a!=0&&b!=0&&c!=0) {
			return 1;
		}
		return 0;
	}

	@Override
	public int del(int id) {
	
		return 0;
	}

	@Override
	public int delbatch(int[] id) {
		return 0;
	}
	
	@RequestMapping("/delbatch")
	public int delbatch(@RequestParam(name="id[]")int[] id,@RequestParam(name="aid[]")int[] aid) {
		//假删老师信息
		int a=teacherService.delbatch(id);
		//锁定账号
		int b=accountService.delbatch(aid);
		
		if(a!=0&&b!=0) {
			return 1;
		}
		return 0;
	}
	
}
